//
//  RightRootViewController.m
//  BProject
//
//  Created by 久远的回忆 on 15/8/11.
//  Copyright (c) 2015年 久远的回忆. All rights reserved.
//

#import "RightRootViewController.h"
#import "MainBaseCollectionViewController.h"
#import "CommonData.h"
@interface RightRootViewController ()

@property (nonatomic, strong) MainBaseCollectionViewController *baseClass;
@property (nonatomic,strong) UINavigationController * navi;
@property (nonatomic,strong) UITapGestureRecognizer *tapGestureRecognizer;
@end

@implementation RightRootViewController


- (void)changeViewWithIndex:(NSInteger)index
{
    //获得公用数据
    NSString *classStr = [CommonData defaultData].classArray[index];
    NSString *titleStr = [CommonData defaultData].titleArray[index];
    //如果点击的是同一页面就不要浪费资源再加载了,返回就可以了
    if ([_baseClass isMemberOfClass:NSClassFromString(classStr)]) {
        [self changeFrame];
        return;
    }
    //把原来的视图移走
    [_navi.view removeFromSuperview];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.
    _baseClass = [[NSClassFromString(classStr) alloc] initWithCollectionViewLayout:layout];
    //_baseClass.titleLabel.text = titleStr;//这个时候的label还是空的.因为实际上并没有走load方法;
    _baseClass.myTitle =titleStr;
    _navi = [[UINavigationController alloc]initWithRootViewController:_baseClass];
    
    _navi.navigationBar.barTintColor = [UIColor blackColor];
    [_navi.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor],NSForegroundColorAttributeName,nil]];
    
    //_navi.navigationBar.tintColor = [UIColor greenColor];
       _baseClass.rightVC = self;
    [self.view addSubview:_navi.view];
    [self changeFrame];
    
}


- (void)changeFrame
{
    CGRect newFrame = self.view.frame;
    //应该在这里添加轻拍手势,
    if (newFrame.origin.x == 0) {
        newFrame.origin.x = SCREEN_WIDTH - 70;
        self.navi.view.userInteractionEnabled = NO;
        [self.view addGestureRecognizer:_tapGestureRecognizer];
    } else {
        //要在这里移除轻怕手势因为手势会影响cell的选中(响应冲突)
        newFrame.origin.x = 0;
        self.navi.view.userInteractionEnabled = YES;
        [self.view removeGestureRecognizer:_tapGestureRecognizer];
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = newFrame;
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
    [self changeViewWithIndex:0];
    [self changeFrame];
    // Do any additional setup after loading the view.
}
- (void)tapBackAction
{
    CGRect newFrame = self.view.frame;
    if (newFrame.origin.x > 200) {
        [self changeFrame];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
